package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import util.Constant;

/**
 */
@WebFilter("/LoginFilter")
public class LoginFilter implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("过滤器已执行!");
		/*HttpSession session = SessionTools.getSession();*/
		HttpServletRequest hsr = (HttpServletRequest)request;
		HttpServletResponse hre = (HttpServletResponse)response;
		HttpSession session = hsr.getSession();
		String path = hsr.getServletPath();
		 /*|| path.contains("javax.faces.resource")*/
		if(session != null && session.getAttribute(Constant.SESSION_USER) != null || path.contains("javax.faces.resource"))
			chain.doFilter(request, response);//放行primefaces的样式
		else{
			System.out.println("path:"+hsr.getServletPath());
			System.out.println("URI:"+hsr.getRequestURI());
			hsr.getRequestDispatcher("login.jsf").forward(request, response);
			//hre.sendRedirect("login.jsf");
		}
		
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
